package com.example.zyfyptlw618.avtivivites;

import android.content.SharedPreferences;
import android.os.Bundle;

import com.google.android.material.bottomnavigation.BottomNavigationView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.view.MenuItem;

import com.example.zyfyptlw618.R;

import java.util.ArrayList;
import java.util.List;

import fragments.BaseFragment;
import fragments.CFragment1;
import fragments.CFragment2;
import fragments.CFragment3;
import fragments.CFragment4;
import fragments.CFragment5;

public class CollectListActivity extends AppCompatActivity {
    private SharedPreferences sp;
    private String sessionID="";

    private ViewPager viewPager;
    private FragmentPagerAdapter fragmentPagerAdapter;
    private BottomNavigationView navigationView;
    private List<BaseFragment> fragmentList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.collect);
        initFragmentList();
        initViewPager();
        initBottomNV();
    }





    private void initBottomNV() {
        navigationView=findViewById(R.id.bottomnv);
        navigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                int id=0;
                switch (item.getItemId()){
                    case R.id.blog2:
                        id=0;
                        break;
                    case R.id.keynote2:
                        id=1;
                        break;
                    case R.id.video2:
                        id=2;
                        break;
                    case R.id.sample2:
                        id=3;
                        break;
                    case R.id.tware:
                        id=4;
                        break;
                }
                System.out.println("---id="+id);
                viewPager.setCurrentItem(id);
                return true;
            }
        });

    }

    private void initFragmentList() {
        fragmentList=new ArrayList<>();
        fragmentList.add(new CFragment1());
        fragmentList.add(new CFragment2());
        fragmentList.add(new CFragment3());
        fragmentList.add(new CFragment4());
        fragmentList.add(new CFragment5());

    }

    private void initViewPager() {
        viewPager=findViewById(R.id.viewPager);
        fragmentPagerAdapter=new FragmentPagerAdapter(getSupportFragmentManager()) {
            @NonNull
            @Override
            public Fragment getItem(int position) {
                return fragmentList.get(position);
            }

            @Override
            public int getCount() {
                return fragmentList.size();
            }
        };

        viewPager.setAdapter(fragmentPagerAdapter);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                System.out.println("--position="+position);
                switch (position){
                    case 0:
                        navigationView.setSelectedItemId(R.id.blog2);
                        break;
                    case 1:
                        navigationView.setSelectedItemId(R.id.keynote2);
                        break;
                    case 2:
                        navigationView.setSelectedItemId(R.id.video2);
                        break;
                    case 3:
                        navigationView.setSelectedItemId(R.id.sample2);
                        break;
                    case 4:
                        navigationView.setSelectedItemId(R.id.tware);
                        break;

                }
            }
            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
    }
}
